<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>订单管理</title>
    <link rel="stylesheet" type="text/css" href="static/admin/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="static/admin/css/admin.css"/>
</head>
<body>
<div class="wrap-container welcome-container">
    <div class="wrap-container clearfix">
        <div class="column-content-detail">
            <div class="layui-form-item">
                <div class="layui-inline" style="margin-bottom: 0;">
                    <input type="text" name="searchphone" required lay-verify="required" placeholder="请输入订单号或者手机号查询"
                           autocomplete="off" class="keywords layui-input" style="width: 220px;">
                </div>
                <button class="layui-btn layui-btn-normal search">搜索</button>
            </div>
            <div class="layui-form" id="table-list">
                <table class="layui-table" lay-even lay-skin="nob">
                    <thead>
                    <tr>
                        <th>订单编号</th>
                        <th>用户</th>
                        <th>总金额（元）</th>
                        <th>物流</th>
                        <th>物流单号</th>
                        <th>创建时间</th>
                        <th>订单状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody class="tabel-content"></tbody>
                </table>
                <div class="page-wrap">
                    <div id="page"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="order-detail-popup">
    <form class="layui-form" id="form">
        <div class="layui-form-item">
            <label class="layui-form-label">订单编号</label>
            <div class="layui-input-inline">
                <input type="text" name="ordernum" data-name="订单编号" required lay-verify="required" disabled
                       autocomplete="off" class="required layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户</label>
            <div class="layui-input-inline">
                <input type="text" name="user" data-name="用户" disabled lay-verify="required" autocomplete="off"
                       class="layui-input required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">总金额</label>
            <div class="layui-input-inline">
                <input type="text" name="totalmoney" data-name="总金额" required lay-verify="required" disabled
                       autocomplete="off" class="layui-input required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-inline">
                <div class="layui-form-item">
                    <input type="text" name="createtime" data-name="创建时间" required lay-verify="required"
                           autocomplete="off" disabled class="layui-input required">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-inline">
                <div class="layui-form-item">
                    <input type="text" name="status" data-name="订单状态" required lay-verify="required" autocomplete="off"
                           disabled class="layui-input required">
                </div>
            </div>
        </div>
        <div class="good-list">
            <table class="layui-table">
                <colgroup>
                    <col width="33%">
                    <col width="33%">
                    <col width="33%">
                </colgroup>
                <thead>
                <tr>
                    <th>商品名称</th>
                    <th>商品价格(元)</th>
                    <th>商品数量</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </form>
</div>

<!--录入物流-->
<div class="wrtinggwuliu">
    <form action="" class="layui-form" id="wuliu">
        <input type="hidden" name="id" id="orderID">
        <div class="layui-form-item">
            <label class="layui-form-label">物流公司</label>
            <div class="layui-input-inline">
                <input type="text" name="logisticsName" data-name="物流公司" required lay-verify="required"
                       placeholder="请输入物流公司"
                       autocomplete="off" class="required layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物流单号</label>
            <div class="layui-input-inline">
                <input type="text" name="waybill" data-name="物流单号" required lay-verify="required" placeholder="请输入物流单号"
                       autocomplete="off" class="required layui-input">
            </div>
        </div>
    </form>
</div>
<script src="static/admin/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="static/admin/js/baseUrl.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'jquery', 'laypage', 'table'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var laypage = layui.laypage;
        var table = layui.table;
        // 请求参数
        var requestData = {
            pageNumber: 1,
            pageSize: 10,
            phone: ''
        };

        // 关闭
        function clearFun() {
            $("#wuliu input").each(function (n) {
                $(this).val('');
            });
        }

        // 数据备份为详情准备
        var DATALIST = null;

        // 订单状态
        var statusText = ['', '待付款', '已付款', '已发货', '已收货'];

        // 状态按钮
        var statusBtnText = ['', '确认付款', '确认发货', '确认收货', '订单完成'];

        // 获取数据
        function getList(params) {
            $.ajax({
                url: baseUrl + '/order/selectOrdersList',
                data: JSON.stringify(params),
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                crossDomain: true,                 //加这二行支持ajax跨域，允许跨域
                xhrFields: {withCredentials: true},//加这二行支持ajax跨域，携带凭证
                success: function (data) {
                    if (data.ret == 200) {
                        var list = data.data.list;
                        DATALIST = list;
                        if (DATALIST.length > 0) {
                            var html = '';
                            for (var item of list) {
                                html += `<tr>
                                    <td class="item-orderid">${item.orderCode}</td>
                                    <td class="item-user">${item.userName}</td>
                                    <td class="item-goodsid">${item.totalCost}</td>
                                    <td class="wuliu">${!item.logisticsName ? '' : item.logisticsName}</td>
                                    <td class="item-goodsprice">${!item.waybill ? '' : item.waybill}</td>
                                    <td>${item.createTime}</td>
                                    <td>${statusText[item.status]}</td>
                                    <td>
                                        <div class="layui-inline">
                                            <button class="layui-btn layui-btn-sm layui-btn-normal see-btn" data-id="${item.id}">查看</button>
                                            <button class="layui-btn  layui-btn-sm layui-btn-normal edit-btn ${item.status == 4 && 'layui-btn-disabled'}" data-status="${item.status}" data-id="${item.id}">
                                                ${statusBtnText[item.status]}
                                            </button>
                                        </div>
                                    </td>
                                </tr>`;
                            }
                            $('.tabel-content').html(html);
                            //执行一个laypage实例
                            laypage.render({
                                elem: 'page',
                                count: data.data.total,
                                curr: requestData.currentNum,
                                jump: function (obj, first) {
                                    if (!first) {
                                        data.pageNumber = obj.curr;
                                        getList(data);
                                    }
                                }
                            });
                        } else {
                            layer.msg('暂无数据')
                        }
                    } else {
                        if (data.ret == 208) {
                            parent.layer.alert(data.msg, {icon: 0}, function () {
                                parent.location.href = 'login.html'
                            })
                        } else {
                            layer.msg(data.msg, {icon: 0, time: 1000});
                        }
                    }
                },
                error: function () {
                    layer.msg('网络发生故障，请稍后再试', {icon: 0, time: 1000});
                },
                complete: function () {
                }
            });
        }

        // 初始数据请求
        getList(requestData);

        /**
         * 提交框发送请求
         * @param id 订单id
         * @param status 订单状态
         */
        function execute(id, status) {
            var loading = layer.load(1, {shade: [0.3, '#000']});
            $.ajax({
                url: baseUrl + '/order/deliveryOrderById',
                data: JSON.stringify({
                    id: id,
                    status: status
                }),
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                crossDomain: true,                 //加这二行支持ajax跨域，允许跨域
                xhrFields: {withCredentials: true},//加这二行支持ajax跨域，携带凭证
                success: function (data) {
                    console.log(data);
                    layer.msg(data.msg, {icon: data.ret == 200 ? 1 : 0, time: 1000}, function () {
                        data.ret == 200 && location.reload();
                    })
                },
                error: function () {
                    layer.msg('网络发生故障，请稍后再试', {icon: 0, time: 1000});
                },
                complete: function () {
                    layer.close(loading);
                }
            })
        }

        // 监听按钮编辑
        $('.tabel-content').on('click', '.edit-btn', function () {
            var status = $(this).data('status');
            var id = $(this).data('id');
            if (status == 1) {
                layer.confirm('确认当前订单已支付吗？', {icon: 0}, function (index) {
                    layer.close(index);
                    execute(id, 2);
                })
            } else if (status == 2) {
                layer.confirm('确认当前订单已发货吗？', {icon: 0}, function (index) {
                    layer.close(index);
                    // console.log(index);
                    layer.open({
                        type: 1,
                        content: $('.wrtinggwuliu'),
                        title: '录入物流信息',
                        maxmin: true,
                        shadeClose: true, //点击遮罩关闭层
                        area: '500px',
                        btn: ['提交', '取消'],
                        yes: function (index, layero) {
                            var sendData = {
                                id: id,
                                logisticsName: $('input[name=logisticsName]').val(),
                                waybill: $('input[name=waybill]').val(),
                                status: 3
                            };
                            var loading = layer.load(1, {shade: [0.3, '#000']});
                            $.ajax({
                                url: baseUrl + '/order/deliveryOrderById',
                                data: JSON.stringify(sendData),
                                type: 'post',
                                dataType: 'json',
                                contentType: 'application/json',
                                crossDomain: true,                 //加这二行支持ajax跨域，允许跨域
                                xhrFields: {withCredentials: true},//加这二行支持ajax跨域，携带凭证
                                success: function (data) {
                                    console.log(data);
                                    layer.msg(data.msg, {icon: data.ret == 200 ? 1 : 0, time: 1000}, function () {
                                        data.ret == 200 && location.reload();
                                    })
                                },
                                error: function () {
                                    layer.msg('网络发生故障，请稍后再试', {icon: 0, time: 1000});
                                },
                                complete: function () {
                                    layer.close(loading);
                                }
                            })
                        },
                        end: function () {
                            clearFun();
                        }

                    })
                })
            } else if (status == 3) {
                layer.confirm('确认当前订单已收货吗？', {icon: 0}, function (index) {
                    layer.close(index);
                    execute(id, 4);
                })
            }
        });

        // 监听搜索功能
        $('button.search').click(function () {
            requestData.phone = $('.keywords').val();
            requestData.pageNumber = 1;
            getList(requestData);
        });

        // 查看订单
        $('.tabel-content').on('click', '.see-btn', function () {
            var id = $(this).data('id');
            var showData = null;
            DATALIST.map(function (item) {
                if (item.id == id) {
                    showData = item;
                    return false;
                }
            })
            console.log(showData);
            $('#form').find('input[name=ordernum]').val(showData.orderCode);
            $('#form').find('input[name=user]').val(showData.userName);
            $('#form').find('input[name=totalmoney]').val(showData.totalCost);
            $('#form').find('input[name=createtime]').val(showData.createTime);
            $('#form').find('input[name=status]').val(statusText[showData.status]);
            var orderListHtml = '';
            for (var item of showData.orderDetailsList) {
                orderListHtml += `<tr>
                    <td>${item.productName}</td>
                    <td>${item.money}</td>
                    <td>${item.count}</td>
                </tr>`
            }
            $('.good-list tbody').html(orderListHtml);
            table.render();
            layer.open({
                type: 1,
                content: $('.order-detail-popup'),
                area: '500px'
            })

        })
    })
</script>
</body>
</html>
